package org.example.controller;



import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;

import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.example.result.Result;
import org.example.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import vo.UploadVo;

import java.util.List;
import java.util.UUID;

@RestController
@Tag(name = "文件上传")
@RequiredArgsConstructor
@Slf4j
public class FileUploadController {

    private final AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    @Operation(summary = "上传文件")
    public Result<UploadVo> upload(MultipartFile file) throws Exception {
        log.info("文件上传：{}", file.getOriginalFilename());
        // 处理上传文件
        String originalFilename = file.getOriginalFilename();
        System.out.println(originalFilename);
        //重构文件名，防止文件名重复
        String fileName = null;
        if (originalFilename != null) {
            fileName = UUID.randomUUID().toString().substring(0, 8) + "-" + originalFilename;
        }
        //调用阿里云oss上传文件
        String url = aliOssUtil.upload(file.getBytes(), fileName);
        UploadVo uploadVo = UploadVo.builder().url(url).build();
        return Result.success(uploadVo);
    }
}
